package oldGui;

import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.swing.JSplitPane;

import editManager.Edit;

import java.awt.BorderLayout;

public class EditManagerMain extends JPanel {

	private JFrame frame;
	private ProcessorPanel processPanel;
	private Timer timer;
	private EditListener eL;
	private long currentTimeStamp;
	private File editFile;

	/**
	 * Create the application.
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	public EditManagerMain() throws IOException, InterruptedException {
		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		setSize(new Dimension(750,550));
		setPreferredSize(new Dimension(300, 600));
		
		
		
		processPanel = new ProcessorPanel();
		add(processPanel);
		
		
		
		editFile = new File("Z:\\AAPRINT-EDIT");
		currentTimeStamp = editFile.lastModified();
		eL = new EditListener();
		timer = new Timer(60000, eL);
		timer.start();

	}

	private void checkUpdate() throws IOException{
		File currentFile = new File("Z:\\AAPRINT-EDIT");
		processPanel.refresh(new Date());
		if(currentFile.lastModified()!=currentTimeStamp){
			int editListTail = processPanel.getEditPanels().size();
			EditPanel eP = new EditPanel(new Date());
			processPanel.getEditPanels().add(eP);
			revalidate();
			repaint();
			sendEmail(processPanel.getEditPanels().get(editListTail).getEditRun().exportTxt());
			currentTimeStamp = currentFile.lastModified();
		}

	}
	
	private void sendEmail(String file){
		String messageText ="";
		ArrayList<Edit> creditEditList = processPanel.getEditPanels().get(processPanel.getEditPanels().size()-1).getEditRun().getCreditList();
		ArrayList<Edit> CCEditList = new ArrayList<Edit>(0);
		ArrayList<Edit> nonCCEditList = new ArrayList<Edit>(0);
		
		for(Edit e : creditEditList){
			if(e.isCreditCard()){
				CCEditList.add(e);
			} else{
				nonCCEditList.add(e);
			}
		}
		
		messageText = "Edits sent to Credit:" + "\n";
		
		for(Edit e : nonCCEditList){
			messageText = messageText + e.getCustomerNumber() + " " + e.getCustomerName() + " " + e.getCutNumber() + "\n";
		}
		
		if(CCEditList.size()>0){
			messageText = messageText + "Credit Cards:" + "\n";
			for(Edit e : CCEditList){
				messageText = messageText + e.getCustomerNumber()+ " " + e.getCustomerName() + " " + e.getCutNumber() + "\n";
			}
		}
		
		final String username = "edits@mariettadrapery.com";
		final String password = "3d!Ts22";
 
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		//props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "mail.mariettadrapery.com");
		props.put("mail.smtp.port", "25");
 
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
		
		DateFormat df = new SimpleDateFormat("HH:mm");
		String formattedDate = df.format(new Date());
		
		try{
	         MimeMessage message = new MimeMessage(session);
	         message.setFrom(new InternetAddress("edits@mariettadrapery.com"));
	         message.addRecipient(Message.RecipientType.TO, new InternetAddress("ar@mariettadrapery.com"));
	         message.addRecipient(Message.RecipientType.CC, new InternetAddress("jedwards@mariettadrapery.com"));
	         message.setSubject("Sent to Credit " + formattedDate);
	         BodyPart messageBodyPart = new MimeBodyPart();
	         messageBodyPart.setText(messageText);
	         Multipart multipart = new MimeMultipart();
	         multipart.addBodyPart(messageBodyPart);
	         messageBodyPart = new MimeBodyPart();
	         String filename = file;
	         DataSource source = new FileDataSource(filename);
	         messageBodyPart.setDataHandler(new DataHandler(source));
	         messageBodyPart.setFileName(filename);
	         multipart.addBodyPart(messageBodyPart);
	         message.setContent(multipart );
	         //Transport.send(message);
	         System.out.println("Sent message successfully....");
	      }catch (MessagingException mex) {
	         mex.printStackTrace();
	      }
		
	}

	private class EditListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()== timer){
				System.out.println("ping timer");
				try {
					checkUpdate();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		
		}
	}
}


